package top.abrsm.service.impl;

import top.abrsm.entity.ProductEntity;
import top.abrsm.mapper.ProductMapper;
import top.abrsm.service.IProductService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import top.abrsm.util.StateEnum;
import top.abrsm.util.StateException;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 商品表 服务实现类
 * </p>
 *
 * @author WuZH
 * @since 2023-11-18
 */
@Service
public class ProductServiceImpl extends ServiceImpl<ProductMapper, ProductEntity> implements IProductService {


    @Resource
    private ProductMapper productMapper;

    @Override
    public List<ProductEntity> queryList ( ProductEntity productEntity ) {
        List<ProductEntity> productEntities = productMapper.queryList ( productEntity.getId (), productEntity.getCategoryId (), productEntity.getProductName (), productEntity.getActualPrice (), productEntity.getIsUsed (), ( productEntity.getPageNum () - 1 ) * productEntity.getPageSize (), productEntity.getPageSize () );
        return productEntities;
    }

    @Override
    public Integer count ( ProductEntity productEntity ) {
        Integer integer = productMapper.queryCount ( productEntity.getId (), productEntity.getCategoryId (), productEntity.getProductName (), productEntity.getIsUsed () );
        return integer;
    }

    @Override
    public ProductEntity querySingle ( ProductEntity productEntity ) {
        ProductEntity product = baseMapper.selectById ( productEntity );
        return product;
    }

    @Override
    public ProductEntity updateSingle ( ProductEntity productEntity ) {
        int i = baseMapper.updateById ( productEntity );
        if ( i == 0 ) {
            throw new StateException ( StateEnum.UPDATE_EXCE );
        }
        return productEntity;
    }

}
